package com.hebeu.group.controller;


import com.hebeu.group.pojo.Cart;
import com.hebeu.group.pojo.Food;
import com.hebeu.group.pojo.custom.CartCustom;
import com.hebeu.group.service.CartService;
import com.hebeu.group.service.FoodService;

import com.hebeu.group.utils.US;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;



import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



import java.util.ArrayList;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private FoodService foodService;


    /**
     * 添加购物车
     * @param food_Id
     * @param user_id
     */
    @RequestMapping(value = "/cart/add")
    @ResponseBody
    public void insert(int food_Id, int user_id) {
        List<Cart> list = cartService.selectAllCarts(user_id);
        //遍历集合
        for (int i = 0; i <= list.size(); i++) {
            //遍历完集合没有符合商品，执行添加新的购物车条目
            if (i >= list.size()) {
                Cart cart = new Cart();
                cart.setFoodCount(1);
                cart.setUserId(user_id);
                cart.setFoodId(food_Id);
                cartService.insert(cart);
                cart.setCartId(new US().getUUID());
                break;
            }
            //判断该用户所拥有的购物车是否已有该商品，若有即更新数量
            Cart c = list.get(i);
            if (food_Id == c.getFoodId()) {
                c.setFoodCount(c.getFoodCount() + 1);
                cartService.updateCart(c);
                break;
            }
        }
    }


    /**
     * 显示购物车
     * @param user_id
     * @return
     */
   @RequestMapping(value = "/cart/list")
   @ResponseBody
    public List<CartCustom> getCartList(int user_id){
        List<Cart> list=cartService.selectAllCarts(user_id);
        ArrayList<CartCustom> cartCustomList=new ArrayList<>();
        for (Cart cart:list) {
            if(cart.getIsEffective()==0){
                CartCustom cartCustom=new CartCustom();
                Food food=foodService.selectSingleFood(cart.getFoodId());
                cartCustom.setFoodName(food.getfName());
                cartCustom.setFoodPrice(food.getfPrice());
                cartCustom.setFoodImgeUrl(food.getfDesc());
                cartCustom.setFoodCount(cart.getFoodCount().intValue());
                cartCustom.setCartId(cart.getCartId());
                cartCustomList.add(cartCustom);
            }
        }
        return  cartCustomList;
    }


    /**
     * 修改菜数量
     * @param food_Count
     * @param cartId
     * @param user_id
     * @return
     */
    @RequestMapping(value = "/update/count")
    @ResponseBody
    public Double updateCount(int food_Count,String cartId,int user_id){
        Double s2=0.00;
        //Double num= Double.parseDouble(food_Count);
        Cart cart=cartService.getSingleCart(cartId);
        cart.setFoodCount(food_Count);
        cartService.updateCart(cart);
        List<Cart> list=cartService.selectAllCarts(user_id);
        for (Cart c:list
        ) {
            Food food = foodService.selectSingleFood(c.getFoodId());
            s2=s2+(c.getFoodCount())*(food.getfPrice());
        }
        return s2;
    }



    /**
     * 删除菜
     * @param cartId
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public void deleteSingleCart(String cartId){
        cartService.deleteCart(cartId);
    }


    /**
     * 清空购物车
     * @param ids
     * @return
     */
    @RequestMapping(value = "/removeCarts")
    @ResponseBody
    public void deleteAllCarts(String ids){
        String[] arr=ids.split(",");
        for (String id:arr
        ) {
            cartService.deleteCart(id);
        }
    }

}
